package top.starcatmeow.encryptedchat;

import android.support.design.widget.TextInputEditText;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * Created by Dongruixuan Li on 2017/1/30.
 */
public class MessageHandler extends Thread {
    Socket socket = null;
    TextInputEditText et = ChatActivity.text;
    boolean isSocket;

    public MessageHandler(Socket socket) {
        isSocket = true;
        this.socket = socket;
    }

    public MessageHandler() {
        isSocket = false;
    }

    @Override
    public void run() {
        if (isSocket) {
            try {
                DataInputStream dis = new DataInputStream(socket.getInputStream());                                     //获取输入流
                while (true) {
                    String tempstr = dis.readUTF();
                    System.out.println(tempstr);
                    ChatActivity.tv.append(AES.getInstance().decrypt(tempstr) + "\n");                                        //解密服务器发送的信息并追加至聊天框末尾
                }
            } catch (IOException e) {
                e.printStackTrace();                                                                                    //显示错误信息
            }
        }
    }

}
